<template>
    <div class="index">
        <c-wrap :title="selected" bgColor="white" :back="false">
            <!--分页面-->
            <div :style="{height:height+'px','overflow-y':'auto'}">
                <my-home ref="myHome" v-if="selected==='首页'" :refresh="refresh"
                         :address="addressStorage?addressStorage:addressLocation"></my-home>
                <my-water v-if="selected==='用量'"></my-water>
                <my-service v-if="selected==='服务'"></my-service>
                <my-account v-if="selected==='我的'"></my-account>
            </div>
            <!--底部tab页-->
            <mt-tabbar fixed v-model="selected">
                <mt-tab-item id="首页">
                    <div>
                        <icon clazz="icon-shouye1"></icon>
                    </div>
                    首页
                </mt-tab-item>
                <mt-tab-item id="用量">
                    <div>
                        <icon clazz="icon-shui"></icon>
                    </div>
                    用量
                </mt-tab-item>
                <mt-tab-item id="服务">
                    <div>
                        <icon clazz="icon-fuwu-"></icon>
                    </div>
                    服务
                </mt-tab-item>
                <mt-tab-item id="我的">
                    <div>
                        <icon clazz="icon-wode"></icon>
                    </div>
                    我的
                </mt-tab-item>
            </mt-tabbar>
        </c-wrap>
    </div>
</template>

<script>
    import CWrap from '../components/CWrap'
    import myHome from './levelOne/myHome'
    import myWater from './levelOne/myWater'
    import myService from './levelOne/myService'
    import myAccount from './levelOne/myAccount'

    export default {
        components: {CWrap, myHome, myWater, myService, myAccount},
        data() {
            return {
                height: app.config.setting.height.display - app.config.setting.height.top - 72 + 15 - app.config.setting.statusHeight,
                selected: this.$route.params.id ? this.$route.params.id : '首页',
                statusHeight: app.config.setting.statusHeight,
                headerColor: app.config.setting.headerColor,
                platform: app.config.setting.platform,
                addressStorage: app.util.localStorage('user.name'),
                addressLocation: '',
                refresh: false
            }
        },
        watch: {
            selected: function (val) {
                if (val === '首页') {
                    this.refresh = true;
                } else {
                    if (!app.util.sessionStorage('token') && this.selected === '我的') {

                        // this.$toast('您还没登录，请重新登录');
                        // setTimeout(() => {
                        this.$router.push({path: '/login'});
                        // }, 3000)
                        return;
                    }
                    this.refresh = false;
                }
            }
        },
        methods: {
            verifyNo() {
                app.ajax.get(app.config.api.householdNo, {}, (res) => {
                    if (res.s === 0) {
                        app.util.localStorage('user.householdNo', res.data.householdNo);
                        app.util.localStorage('user.address', res.data.address);
                        app.util.localStorage('user.name', res.data.name);
                        app.util.localStorage('user.chargeWayTxt', res.data.chargeWayTxt);
                        let ladderVolume = res.data.ladderVolume.split(',');
                        app.util.localStorage('user.ladderVolumeFirst', ladderVolume[0]);
                        app.util.localStorage('user.ladderVolumeSecond', ladderVolume[1]);
                        app.util.localStorage('user.ladderVolumeThree', ladderVolume[2]);
                        this.addressLocation = res.data.address;
                        this.$refs['myHome'].loadData();
                    }
                }, (err) => {
                    setTimeout(() => {
                        if (err.data.msg === '户号未绑定') {
                            this.$router.push({name: 'accountBinding02'});
                        }
                    }, 500)
                })
            }
        },
        // created() {
        //     if (this.platform === 'mobile') {
        //         if (app.util.localStorage('token')) app.util.sessionStorage('token', app.util.localStorage('token'));
        //     }
        // },
        mounted() {
            if (this.platform === "wechat" && !app.util.sessionStorage('token')) {
                let token = window.location.href.split('?')[1];
                app.util.sessionStorage('token', token.split('=')[1]);
                // 判断是否绑定户号
                if (!app.util.localStorage('user.householdNo')) {
                    this.verifyNo()
                } else {
                    if (this.selected === '首页')
                        this.$refs['myHome'].loadData();
                }
            } else if (this.platform === 'mobile') {
                if (app.util.localStorage('token')) {
                    app.util.sessionStorage('token', app.util.localStorage('token'));
                    if (new Date().getTime() - app.util.localStorage('tokenTime') > 288000000) {
                        app.util.sessionStorage('token', '');
                        this.$toast('登录时间过长，请重新登录');
                        setTimeout(() => {
                            this.$router.push({path: '/login'});
                        }, 3000)
                    } else {
                        if (app.util.localStorage('user.householdNo')) {
                            if (this.selected === '首页')
                                this.$refs['myHome'].loadData();
                        } else {
                            this.verifyNo();
                        }
                    }
                }
                app.util.localStorage('tokenTime', new Date().getTime());
            } else {
                if (!app.util.sessionStorage('token')) {
                    // this.$router.push({path: '/login'});
                } else if (!app.util.localStorage('user.householdNo')) { // 判断是否绑定户号
                    this.verifyNo();
                } else {
                    if (this.selected === '首页')
                        this.$refs['myHome'].loadData();
                }
            }
        }
    }
</script>
